const { ListNode, List, List2Array } = require('./common')

/* 迭代 */
/* function reverseList(head) {
  let pre = null
  let cur = head
  while (cur) {
    let tmp = cur.next
    cur.next = pre
    pre = cur
    cur = tmp
  }
  return pre
} */
/* 递归 */
function reverseList(head) {
  if (head === null || head.next === null) {
    return head
  }

  const p = reverseList(head.next)

  head.next.next = head
  head.next = null

  return p

}

console.log(List2Array(reverseList(List([1, 2, 3, 4]))))

